package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.dto.UserListQryDto;
import com.bsh.mybatis.model.User;
import com.bsh.mybatis.model.UserVerifyInfo;
import com.bsh.repository.DaoUser;
import com.bsh.repository.DaoUserVerifyInfo;
import com.bsh.service.UserVerifyService;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 用户验证
 * 
 * @author gsh
 *
 */
@Service
@Transactional
public class UserVerifyServiceImpl implements UserVerifyService {
	@Autowired
	private DaoUserVerifyInfo veriDao;
	@Autowired
	DaoUser  userDao;
	
	@Override
	public List<Map<String, Object>> getPage(Pager pager, UserListQryDto dto) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isNullOrEmpty(dto.getName()))
			sb.append(String.format(" and u.name like '%%%s%%'",dto.getName().trim()));
		if (!StringUtils.isNullOrEmpty(dto.getMobile()))
			sb.append(String.format(" and u.mobile like '%%%s%%'",dto.getMobile().trim()));
		if(dto.getIdcardVerifyStatus()!=null && dto.getIdcardVerifyStatus()!=-1){
			if(dto.getIdcardVerifyStatus()==1)
				sb.append(" and v.is_pass=1 ");
			else
				sb.append(" and v.is_pass<>1 ");
		}
		if(dto.getPicVerifyStatus()!=null && dto.getPicVerifyStatus()!=-1){
			if(dto.getPicVerifyStatus()==3)
				sb.append(" and v.audit_status is null ");
			else
				sb.append(String.format(" and v.audit_status=%s ",dto.getPicVerifyStatus()));
		}
		
		List<Map<String, Object>> list = veriDao.getPage(pager, sb.toString());
		return list;
	}

	@Override
	public int saveUserVerifyInfo(UserVerifyInfo record) {
		User user = userDao.selectByPrimaryKey(record.getUserid());
		user.setRealName(record.getName());
		user.setSex(record.getSex());
		userDao.updateByPrimaryKey(user);
		return veriDao.insertUserVerifyInfo(record);
	}

	@Override
	public boolean checkUserVerify(Integer userid) {
		List<Map<String, Object>> list = veriDao.getVerifyInfo(userid);
        return list.size() > 0;

	}

	@Override
	public String getUserVerify(Integer userid) {
		List<Map<String, Object>> list = veriDao.getVerifyInfo(userid);
		if (list.size() > 0) {
			return  (String) list.get(0).get("idcard");
		} else {
			return "";
		}
	}

	@Override
	public JSONObject getUserVerifyInfo(Integer userid) {
		List<Map<String, Object>> list = veriDao.getVerifyInfo(userid);
		JSONObject result=new JSONObject();
		if (list.size() > 0) {
			Map<String, Object> map=list.get(0);
			result.put("id",map.get("id"));
			result.put("real_name",map.get("name"));
			result.put("id_card", map.get("idcard"));
			result.put("sex", map.get("sex"));
			result.put("is_pass",true);
			result.put("pic_url",ApiUtils.objIsNotEmpty(map.get("pic_url"))?map.get("pic_url"):"-1");
			result.put("audit_status",ApiUtils.objIsNotEmpty(map.get("audit_status"))?map.get("audit_status"):"-1");
		}
		return result;
	}

	@Override
	public int audit(int id, boolean isPass, String auditMsg) {
	    UserVerifyInfo veri = veriDao.selectByPrimaryKey(id);
		if (isPass){
			int res=veriDao.auditPass(id, true);		
			return res;
		}
		else{
			int res=veriDao.auditRefuse(id, false, auditMsg);
			return res;
		}
	}

	
}
